package com.sinodata.bsm.center.web.action.test;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.cache.DefaultEventRuleCache;
import com.sinodata.bsm.center.cache.EventRuleCache;
import com.sinodata.bsm.center.cache.EventTypeCache;
import com.sinodata.bsm.center.cache.ResCache;
import com.sinodata.bsm.center.cache.ResTypeCache;
import com.sinodata.bsm.center.expression.Expressions;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.utils.StringUtil;
import com.sinodata.bsm.common.vo.DefaultEventRule;
import com.sinodata.bsm.common.vo.EventRule;
import com.sinodata.bsm.common.vo.EventType;
import com.sinodata.bsm.common.vo.Res;

/**
 * <p>
 * Description: 事件规则测试
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-11-19 14:33:28      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class EventRuleTestAction<T> extends BaseAction<Object> {

    @Autowired
    private ResCache resCache;

    @Autowired
    private ResTypeCache resTypeCache;

    @Autowired
    private DefaultEventRuleCache defaultEventRuleCache;

    @Autowired
    private EventRuleCache eventRuleCache;

    @Autowired
    private EventTypeCache eventTypeCache;

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.ModelDriven#getModel()
     */
    @Override
    public Object getModel() {
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#list()
     */
    @Override
    public String list() throws Exception {
        List<Res> resList = resCache.findAll();
        for (Res res : resList) {
            Long resTypeId = res.getResTypeId();
            List<DefaultEventRule> defaultEventRuleList = new ArrayList<DefaultEventRule>();
            getDefaultEventRuleList(defaultEventRuleList, resTypeId);
            for (DefaultEventRule defaultEventRule : defaultEventRuleList) {
                //转换后关联指标和注册到引擎
                EventRule eventRule = TransEventRule(defaultEventRule, res.getId());
                if (!Test(eventRule.getExpr())) {
                    System.out.println("规则表达式错误" + defaultEventRule.getId());
                }
                if (!Test(eventRule.getMsg())) {
                    System.out.println("消息表达式错误" + defaultEventRule.getId());
                }
            }
        }
        //检测事件规则
        List<EventRule> eventRuleList = eventRuleCache.getAll();
        if (eventRuleList != null) {
            for (EventRule eventRule : eventRuleList) {
                if (!Test(eventRule.getExpr())) {
                    System.out.println("规则表达式错误" + eventRule.getId());
                }
                if (!Test(eventRule.getMsg())) {
                    System.out.println("消息表达式错误" + eventRule.getId());
                }
            }
        }
        //检测事件类别的事件规则
        List<EventType> eventTytpeList = eventTypeCache.getAll();
        for (EventType eventType : eventTytpeList) {
            Long resTypeId = eventType.getResTypeId();
            Long resId = eventType.getResId();
            if (resId == null && resTypeId != null) {
                resId = getFirstResId(resTypeId);
            }
            if (resId == null) {
                continue;
            }
            String expr = eventType.getExpr();
            expr = StringUtil.replace(expr, "$resid", resId + "");
            expr = StringUtil.replace(expr, "$var", "0");
            if (!Test(expr)) {
                System.out.println("规则表达式错误");
            }
            String msg = StringUtil.replace(eventType.getMsg(), "$resid", resId + "");
            if (!Test(msg)) {
                System.out.println("消息表达式错误");
            }
        }
        return null;
    }

    /**
     * 获取系统中任意一个符合类别的资源，找到即不循环，没有返回null.
     * 
     * @return
     */
    private Long getFirstResId(Long resTypeId) {
        List<Res> resList = resCache.findAll();
        for (Res res : resList) {
            if (res.getResTypeId().longValue() == resTypeId.longValue()) {
                return res.getId();
            }
        }
        return null;
    }

    /**
     * 获取默认的事件规则，包含继承的事件规则
     * 
     * @param result
     * @param resTypeId
     * @return
     */
    private List<DefaultEventRule> getDefaultEventRuleList(List<DefaultEventRule> result, Long resTypeId) {
        Long parentId = resTypeCache.get(resTypeId).getParentId();
        if (parentId != null) {
            getDefaultEventRuleList(result, parentId);
        }
        List<DefaultEventRule> temp = defaultEventRuleCache.getDefaultEventRuleByResTypeId(resTypeId);
        if (temp != null && temp.size() > 0) {
            result.addAll(temp);
        }
        return result;
    }

    /**
     * 将默认事件规则转换为事件规则
     * 
     * @param defaultEventRule
     * @param resId
     * @return
     */
    private EventRule TransEventRule(DefaultEventRule defaultEventRule, Long resId) {
        EventRule eventRule = new EventRule();
        eventRule.setResId(resId);
        eventRule.setEventTypeId(defaultEventRule.getEventTypeId());
        eventRule.setLevel(defaultEventRule.getLevel());
        String expr = StringUtil.replace(defaultEventRule.getExpr(), "$resid", resId + "");
        eventRule.setExpr(expr);
        if (defaultEventRule.getDuration() != null) {
            eventRule.setDuration(defaultEventRule.getDuration());
        } else {
            eventRule.setDuration(0);
        }
        String msg = StringUtil.replace(defaultEventRule.getMsg(), "$resid", resId + "");
        eventRule.setMsg(msg);
        eventRule.setRemarks(defaultEventRule.getRemarks());
        eventRule.setVailTime(defaultEventRule.getVailTime());
        // 是否默认规则表达式，消息表达式不设值
        eventRule.setFilterDuration(defaultEventRule.getFilterDuration());
        eventRule.setConfirmDuration(defaultEventRule.getConfirmDuration());
        eventRule.setKeyWord(defaultEventRule.getKeyWord());
        return eventRule;
    }

    /**
     * 测试表达式，当系统中无值的情况下，给的默认值并不能完全模拟表达式是否完全正确
     * 
     * @param expr
     * @return
     */
    private boolean Test(String expr) {
        try {
            Object o = Expressions.eval(expr);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#input()
     */
    @Override
    public String input() throws Exception {

        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#save()
     */
    @Override
    public String save() throws Exception {
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#delete()
     */
    @Override
    public String delete() throws Exception {
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#prepareModel()
     */
    @Override
    protected void prepareModel() throws Exception {

    }

}